<!DOCTYPE html>
<html class="ui-page-login">
	<head>
		<meta charset="utf-8">
		<meta name="viewport"
			content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no" />
		<title></title>
		<link href="css/mui.min.css" rel="stylesheet" />
		<link rel="stylesheet" type="text/css" href="css/iconfonts.css" />
		<!-- <link href="css/style.css" rel="stylesheet" /> -->
		<link rel="stylesheet" type="text/css" href="css/login.css" />
		<script type="text/javascript">
			window.onload = function() {
				setInterval(function() {
					var companyname = localStorage.getItem("copanyName");
					if (companyname == null) {
						document.getElementById("authorizedUnit").innerText = "";
					}
					document.getElementById("authorizedUnit").innerText = companyname;
				}, 500)
			}
		</script>
	</head>
	<body>
		<header class="mui-bar mui-bar-nav">
			<h1 class="mui-title">登录</h1>
		</header>
		<div class="mui-content">
			<div class="topImg">
				<div class="logo">
					<img src="images/logo.png">
				</div>
				<div class="textN">
					金财云自来水抄表App
				</div>
			</div>
			<div class="logins">
				<div class="update">
					<div class="imgBox">
						<span class="icon iconfont">&#xe602;</span>
					</div>
					<div class="userInfo staff">
						<input id="account" type="text" placeholder="登录名">
					</div>
				</div>
				<div class="update">
					<div class="imgBox">
						<span class="icon iconfont">&#xe60b;</span>
					</div>
					<div class="userInfo mui-password">
						<input id="password" type="password" class="mui-input-password" placeholder="密码">
					</div>
				</div>
				<button id="login" type="button" class="mui-btn mui-btn-primary mui-btn-block">登录</button>
				<div class="link-area"><a id="forgetPassword">忘记密码</a></div>
				<div class="serverURLManage">
					<a href="javascript:;" id="urlManage">
						服务器设置
					</a>
				</div>
				<div class="authorizedCompany">
					<span id="">
						授权单位：
					</span>
					<span id="authorizedUnit">

					</span>
				</div>
			</div>
			<div class="bottomImg">
				<span class="push Introduction">江西金财软件科技有限公司</span>
				<span class="push">服务热线：0797-8174338</span>
				<span class="push">销售热线：013879706198</span>
				<!-- <span class="push" id="corporator"></span> -->
			</div>
		</div>

		<script src="js/mui.min.js"></script>
		<script src="js/mui.enterfocus.js"></script>
		<script src="js/jquery-2.1.1.min.js" type="text/javascript" charset="utf-8"></script>
		<!-- <script src="http://103.59.149.116:6198/hcys/js/mapi.js" type="text/javascript" charset="utf-8"></script> -->
		<script src="js/Mobile.js" type="text/javascript" charset="utf-8"></script>
		<script src="js/app.js"></script>
		<script>
			// 判断网络连接状态
			function isNetWork() {
				var NetStateStr = '未知';
				var types = {};
				types[plus.networkinfo.CONNECTION_UNKNOW] = "未知";
				types[plus.networkinfo.CONNECTION_NONE] = "未连接网络";
				types[plus.networkinfo.CONNECTION_ETHERNET] = "有线网络";
				types[plus.networkinfo.CONNECTION_WIFI] = "WiFi网络";
				types[plus.networkinfo.CONNECTION_CELL2G] = "2G蜂窝网络";
				types[plus.networkinfo.CONNECTION_CELL3G] = "3G蜂窝网络";
				types[plus.networkinfo.CONNECTION_CELL4G] = "4G蜂窝网络";
				NetStateStr = types[plus.networkinfo.getCurrentType()];
				return (NetStateStr === "未知") || (NetStateStr === "未连接网络") ? false : true;
			}
		
			//服务器地址管理
			mui(".serverURLManage").on("tap", "#urlManage", function() {
				// 判断网络是否连接
				if (!isNetWork()) {
					mui.toast('请检查网络连接状态');
					return;
				}
				var checkCode = localStorage.getItem("checkCodeValue");
				mui.openWindow({
					url: "serverURLManage.html",
					id: "serverURLManage",
					extras:{
						checkCode: checkCode
					}
				})
			})

			// 获取授权单位名
			// window.addEventListener('companyName',function(event){
			//   //获得事件参数
			//   var companyname = event.detail.companyname;
			// 	// console.log(companyname);
			//   $("#authorizedUnit").text(companyname);
			// });
			

			mui.plusReady(function() {
				var loginPage = plus.webview.currentWebview();
				// loginPage.addEventListener("loaded", function(){
				var myStorage = JSON.parse(localStorage.getItem("users"));
				// alert(myStorage);
				// var glbToken = myStorage.glbToken;
				if (myStorage == null) {
					return;
					// loginPage.addEventListener("loaded", function(){
					// 	alert("请您登录")
					// })
				} else {
					setTimeout(function() {
						var glbToken = myStorage.glbToken;
						var mainPage = plus.webview.getWebviewById("main");
						var tokenStatus = checkToken(glbToken);
						var main_loaded_flag = false;
						// var glbTo = "token"
						if (!mainPage) {
							mainPage = mui.preload({
								"id": 'main',
								"url": 'main.html'
							});
						} else {
							main_loaded_flag = true;
						}

						mainPage.addEventListener("loaded", function() {
							main_loaded_flag = true;
						});
						var toMain = function() {
							//使用定时器的原因：
							//可能执行太快，main页面loaded事件尚未触发就执行自定义事件，此时必然会失败
							var id = setInterval(function() {
								if (main_loaded_flag) {
									clearInterval(id);
									mui.fire(mainPage, 'show', null);
									mainPage.show("pop-in");
								}
							}, 20);
						};
						if (tokenStatus.status == "ok") {
							toMain();
						} else {
							// toMain();
							plus.nativeUI.toast();
							return;
						}
					}, 1000)
				}
			})
			// jQuery.noConflict();
			(function($, doc) {
				$.init({
					statusBarBackground: '#f7f7f7'
				});
				$.plusReady(function() {
					plus.screen.lockOrientation("portrait-primary");
					var settings = app.getSettings();
					var state = app.getState();
					var mainPage = plus.webview.getWebviewById("main");


					// var loginButton = doc.getElementById('login');
					// loginButton.addEventListener('tap', function(event) {
					// 	console.log(accountBox.value);
					// 	console.log(passwordBox.value);
					// 	var loginInfo = {
					// 		account: accountBox.value,
					// 		password: passwordBox.value
					// 	};
					// 	app.login(loginInfo, function(err) {
					// 		if (err) {
					// 			plus.nativeUI.toast(err);
					// 			return;
					// 		}
					// 		toMain();
					// 		// alert(glbToken)
					// 	});
					// });

					var main_loaded_flag = false;
					if (!mainPage) {
						mainPage = $.preload({
							"id": 'main',
							"url": 'main.html'
						});
					} else {
						main_loaded_flag = true;
					}

					mainPage.addEventListener("loaded", function() {
						main_loaded_flag = true;
					});
					var toMain = function() {
						//使用定时器的原因：
						//可能执行太快，main页面loaded事件尚未触发就执行自定义事件，此时必然会失败
						var id = setInterval(function() {
							if (main_loaded_flag) {
								clearInterval(id);
								$.fire(mainPage, 'show', null);
								mainPage.show("pop-in");
							}
						}, 20);
					};
					//检查 "登录状态/锁屏状态" 开始
					if (settings.autoLogin && state.token && settings.gestures) {
						$.openWindow({
							url: 'unlock.html',
							id: 'unlock',
							show: {
								aniShow: 'pop-in'
							},
							waiting: {
								autoShow: false
							}
						});
					} else if (settings.autoLogin && state.token) {
						toMain();
					} else {
						app.setState(null);
					}
					// close splash
					setTimeout(function() {
						//关闭 splash
						plus.navigator.closeSplashscreen();
					}, 600);
					//检查 "登录状态/锁屏状态" 结束
					var loginButton = doc.getElementById('login');

					var forgetButton = doc.getElementById('forgetPassword');
					loginButton.addEventListener('tap', function(event) {
						var serverURL = localStorage.getItem("serverURL");
						if (serverURL == null) {
							plus.nativeUI.toast("请先设置服务器");
							return;
						}
						// 判断网络是否连接
						if (!isNetWork()) {
							mui.toast('请检查网络连接状态');
							return;
						}
							
						var accountBox = doc.getElementById('account');
						var passwordBox = doc.getElementById('password');
						var loginInfo = {
							account: accountBox.value,
							password: passwordBox.value
						};
						app.login(loginInfo, function(err) {
							if (err) {
								plus.nativeUI.toast(err);
								return;
							}
							toMain();
						});
					});
					$.enterfocus('#login-form input', function() {
						$.trigger(loginButton, 'tap');
					});
					forgetButton.addEventListener('tap', function(event) {
						$.openWindow({
							url: 'forget_password.html',
							id: 'forget_password',
							preload: true,
							show: {
								aniShow: 'pop-in'
							},
							styles: {
								popGesture: 'hide'
							},
							waiting: {
								autoShow: false
							}
						});
					}, false);
					//
					// window.addEventListener('resize', function() {
					// 	oauthArea.style.display = document.body.clientHeight > 400 ? 'block' : 'none';
					// }, false);
					//
					var backButtonPress = 0;
					$.back = function(event) {
						backButtonPress++;
						if (backButtonPress > 1) {
							plus.runtime.quit();
						} else {
							plus.nativeUI.toast('再按一次退出应用');
						}
						setTimeout(function() {
							backButtonPress = 0;
						}, 1000);
						return false;
					};
				});
			}(mui, document));
		</script>
	</body>

</html>
